package com.derbysoft.lowcostadapter.commons.redis.service.impl;

import com.derbysoft.lowcostadapter.commons.model.RoomType;
import com.derbysoft.lowcostadapter.commons.redis.service.JedisClusterService;
import com.derbysoft.lowcostadapter.commons.redis.service.RoomTypeCacheService;
import com.derbysoft.lowcostadapter.commons.redis.utils.KeyValueHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by : jason
 * DateTime: 12-2-10
 */
@Component("roomTypeCacheService")
public class RoomTypeCacheServiceImpl implements RoomTypeCacheService {
    @Autowired
    private JedisClusterService jedisClusterService;

    @Override
    public RoomType load(String key) {
        String value = jedisClusterService.get(key);
        if (value == null) {
            return null;
        }
        RoomType roomType = new RoomType();
        roomType.setPropertyRoomTypeID(Integer.parseInt(key.split(KeyValueHelper.SEPARATOR)[1]));
        roomType.setRoomTypeCode(value.split(KeyValueHelper.SEPARATOR)[0]);
        roomType.setDescription(value.split(KeyValueHelper.SEPARATOR)[1]);
        return roomType;
    }

    @Override
    public RoomType loadByRoomTypeId(int roomTypeId) {
        return load(RoomType.PREFIX + KeyValueHelper.SEPARATOR + roomTypeId);
    }

    @Override
    public void saveAll(List<RoomType> roomTypes) {
        List<SetEntity> setEntities = new ArrayList<SetEntity>();
        for (RoomType roomType : roomTypes) {
            setEntities.add(SetEntity.build(roomType.toKey(), roomType.value()));
        }
        jedisClusterService.saveSetEntities(setEntities);
    }
}
